package yagwl.service.demo;

import java.io.Serializable;
import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import yagwl.service.util.CryptoUtil;
import yagwl.service.util.NumberUtil;

@SuppressWarnings("serial")
@Entity
@Table(name = "PRODUCT")
public class ProductEntity implements Serializable {

  private static final String entityCode = "PRD";

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "PRODUCT_ID")
  private Long productId;

  /*
   * ManyToMany not currently supported on App Engine
   */
  // @ManyToMany(mappedBy = "products")
  // private List<ProductCategoryEntity> categories;

  @Column(name = "NAME")
  private String name;

  @Column(name = "DESCRIPTION")
  private String description;

  @Column(name = "IMAGE_URL")
  private String imageUrl;

  @Column(name = "PRICE")
  private float price;

  @Column(name = "FLEX_1")
  private String field1;

  @Column(name = "FLEX_2")
  private String field2;

  @Column(name = "FLEX_3")
  private String field3;

  @Column(name = "FLEX_4")
  private String field4;

  @Column(name = "FLEX_5")
  private String field5;

  @Column(name = "FLEX_6")
  private String field6;

  Long getProductId() {
    return productId;
  }

  void setProductId(Long productId) {
    this.productId = productId;
  }

  public String getEncryptedProductId() {
    return encryptId(productId);
  }

  public void setEncryptedProductId(String id) {
    productId = decryptId(id);
  }
  
  static String encryptId(Long id) {
    return CryptoUtil.encryptLong(id, entityCode);
  }

  static Long decryptId(String id) {
    return CryptoUtil.decryptLong(id, entityCode);
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String getImageUrl() {
    return imageUrl;
  }

  public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
  }

  public float getPrice() {
    return price;
  }

  public void setPrice(float price) {
    this.price = price;
  }

  public String getPriceStr() {
    return NumberUtil.toString(price, Locale.US);
  }

  public void setPriceStr(String price) {
    this.price = NumberUtil.toFloatValue(price);
  }

  public String getPriceWithCurrency() {
    return NumberUtil.toCurrencyString(price, Locale.US);
  }

  public String getField1() {
    return field1;
  }

  public void setField1(String field1) {
    this.field1 = field1;
  }

  public String getField2() {
    return field2;
  }

  public void setField2(String field2) {
    this.field2 = field2;
  }

  public String getField3() {
    return field3;
  }

  public void setField3(String field3) {
    this.field3 = field3;
  }

  public String getField4() {
    return field4;
  }

  public void setField4(String field4) {
    this.field4 = field4;
  }

  public String getField5() {
    return field5;
  }

  public void setField5(String field5) {
    this.field5 = field5;
  }

  public String getField6() {
    return field6;
  }

  public void setField6(String field6) {
    this.field6 = field6;
  }

  String print() {
    return "" + productId + ":" + name;
  }

}
